package com.project.bookstore.order.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.project.bookstore.common.CommonResult;
import com.project.bookstore.order.pojo.OrderCreateVO;
import com.project.bookstore.order.pojo.OrderDetailVO;
import com.project.bookstore.order.pojo.OrderMultiConditionDTO;
import com.project.bookstore.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(tags="订单模块")
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    OrderService orderService;

    @ApiOperation("订单列表")
    @ApiOperationSupport(order = 10)
    @GetMapping("/{userId}")
    public CommonResult<List<OrderDetailVO>> orderList(@PathVariable Long userId) {
        return orderService.orderList(userId);
    }

    @ApiOperation("统计订单状态")
    @ApiOperationSupport(order = 11)
    @GetMapping("/countOrderStates/{userId}")
    public CommonResult<HashMap<String, Integer>> countState(
            @PathVariable Long userId
    ) {
        return orderService.countState(userId);
    }

    @ApiOperation("多条件查询")
    @ApiOperationSupport(order = 20)
    @PostMapping("/multiCondition/{userId}")
    public CommonResult<List<OrderDetailVO>> orderMultiCondition(
            @PathVariable Long userId,
            @RequestBody OrderMultiConditionDTO conditionDTO) {
        System.out.println("conditionDTO = " + conditionDTO);
        return orderService.orderMultiCondition(userId,conditionDTO);
    }

    @ApiOperation("创建订单")
    @ApiOperationSupport(order = 21)
    @PostMapping("/create")
    public CommonResult createOrder(@RequestBody OrderCreateVO orderCreateVO){
        log.debug("创建订单收到数据：【{}】",orderCreateVO);
        return orderService.createOrder(orderCreateVO);
    }

    @ApiOperation("通过状态查询订单")
    @ApiOperationSupport(order = 12)
    @GetMapping("/{userId}/{state}")
    public CommonResult<List<OrderDetailVO>> getByState(
            @PathVariable("userId")Long userId
            ,@PathVariable("state")Integer state){
        System.out.println("state = " + state);
        OrderMultiConditionDTO stateCondition = new OrderMultiConditionDTO();
        stateCondition.setState(state);
        return orderService.orderMultiCondition(userId,stateCondition);
    }
}
